<?php

namespace app\common\model\order;

use app\common\model\BaseModel;

class Order extends BaseModel
{
    protected $name = 'order';

    protected $pk = 'id';
    protected $autoWriteTimestamp = 'int';
    protected $createTime = 'add_time';
    protected $updateTime = false;

    protected $type = [
        'add_time' => 'timestamp',
        'go_date' => 'timestamp',
    ];

    /**
     * 字段验证规则
     * @var array
     */
    protected $schema = [
        'id'            => 'int',
        'order_sn'      => 'string',
        'user_id'       => 'int',
        'orderable_id'  => 'int',
        'orderable_type' => 'string',
        'order_sn'      => 'string',
        'status'        => 'int',
        'total_amount'  => 'float',
        'actual_amount' => 'float',
        'add_time'      => 'timestamp',
        'pay_time'      => 'timestamp',
        'go_date'       => 'timestamp',
        'is_delete'     => 'int',
        'remark'        => 'string',
        'user_ip'       => 'string',
        'transaction_id' => 'string',
    ];

    const UN_DELED = 0;
    const IS_DELED = 1;
    const DELAY_TIME = 600;
    const UN_PAID = 1;
    const IS_PAID = 2;
    const ORDER_OUT = 3;
    const REFUND_APPLY = 4;
    const IS_APPLIED = 5;
    const REFUND_FAIL = 6;
    const REJECT_REFUND = 7;
    const IS_COMPLETE = 8;
    const IS_CLOSED = 9;
    const IS_COMMENT = 10;
    const ORDER_TYPES = [
        [
            'id' => 0,
            'label' => '全部'
        ],
        [
            'id' => self::UN_PAID,
            'label' => '待支付'
        ],
        [
            'id' => self::IS_PAID,
            'label' => '已支付'
        ],
        [
            'id' => self::ORDER_OUT,
            'label' => '已出单',
        ],
        [
            'id' => self::REFUND_APPLY,
            'label' => '退款申请',
        ],
        [
            'id' => self::IS_APPLIED,
            'label' => '已退款',
        ],
        [
            'id' => self::REFUND_FAIL,
            'label' => '退款失败',
        ],
        [
            'id' => self::REJECT_REFUND,
            'label' => '拒绝退款',
        ],
        [
            'id' => self::IS_COMPLETE,
            'label' => '已完成',
        ],
        [
            'id' => self::IS_CLOSED,
            'label' => '已关闭',
        ],
        [
            'id' => self::IS_COMMENT,
            'label' => '已评论',
        ]
    ];
    const ORDER_TYPES_SHOW = [
        [
            'id' => 0,
            'label' => '全部'
        ],
        [
            'id' => self::UN_PAID,
            'label' => '待支付'
        ],
        [
            'id' => self::IS_PAID,
            'label' => '待出单'
        ],
        [
            'id' => self::IS_COMPLETE,
            'label' => '已完成',
        ],
        [
            'id' => self::IS_COMMENT,
            'label' => '已评论',
        ]
    ];

    /**
     * 多态关联 - 关联具体的订单类型
     * @return \think\model\relation\MorphTo
     */
    public function orderable()
    {
        return $this->morphTo('orderable_type', [
            'ship' => ShipOrder::class,
            'hotel' => HotelOrder::class,
            'goods' => GoodsOrder::class,
        ]);
    }

    /**
     * 获取订单状态标签
     * @return string
     */
    public function getStatusLabelAttribute()
    {
        $statusMap = [
            self::UN_PAID => '待支付',
            self::IS_PAID => '已支付',
            self::ORDER_OUT => '已出单',
            self::REFUND_APPLY => '退款申请',
            self::IS_APPLIED => '已退款',
            self::REFUND_FAIL => '退款失败',
            self::REJECT_REFUND => '拒绝退款',
            self::IS_COMPLETE => '已完成',
            self::IS_CLOSED => '已关闭',
            self::IS_COMMENT => '已评论',
        ];

        return $statusMap[$this->status] ?? '未知状态';
    }

    /**
     * 获取具体订单详情
     * @return mixed
     */
    public function getDetailAttribute()
    {
        return $this->orderable;
    }

    /**
     * 根据订单类型获取对应的订单模型
     * @param string $type 订单类型: ship, hotel, goods
     * @return string|null
     */
    public static function getOrderableModel(string $type): ?string
    {
        $models = [
            'ship' => ShipOrder::class,
            'hotel' => HotelOrder::class,
            'goods' => GoodsOrder::class,
        ];

        return $models[$type] ?? null;
    }

    /**
     * 创建订单时设置多态关联
     * @param Model $orderable 具体订单模型实例
     * @return $this
     */
    public function setOrderable(BaseModel $orderable)
    {
        $this->orderable()->associate($orderable->getData());
        return $this;
    }

    /**
     * 作用域：按订单类型筛选
     * @param \think\db\Query $query
     * @param string $type 订单类型
     */
    public function scopeType($query, string $type)
    {
        $query->where('orderable_type', $type);
    }
}
